/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-11-11 15:52:48
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-02-14 09:52:36
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { LazyLoadEvent, TreeNode } from 'primeng/api';
import { BomContentServiceProxy, BopNodeServiceProxy, ProductionDesignServiceProxy, BopExportInput } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';


@Component({
    selector: 'app-bop-export',
    templateUrl: './bop-export.component.html',
    styleUrls: ['./bop-export.component.less']
})
export class BopExportComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;//行数据
    saving = false;
    active = false;
    loadingtree: boolean;
    files2: any;
    files1: any;
    loadingTable: boolean;
    selectedFiles: any = {};
    selectRow = [];//导出勾选
    materialCode = undefined;
    materialname = undefined;
    saveFlag = true;
    propty = undefined;
    proptyList = [
        {
            label: '已导出',
            value: true
        }, {
            label: '未导出',
            value: false
        }
    ]
    constructor(
        injector: Injector,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _messageService: MessageService,
        private _bopNodeServiceProxy: BopNodeServiceProxy,
        private _productionDesignServiceProxy: ProductionDesignServiceProxy,
    ) {
        super(injector);
    }
    export() {
        let input = new BopExportInput();
        input.lotNoId = this.editData.lotNoId;
        input.modelId = this.editData.modelId;
        input.techIdList = [];
        for (const item of this.selectRow) {
            input.techIdList.push(item.id);
        }
        this.message.confirm(
            '确认要导出吗？',
            (isConfirmed) => {
                if (isConfirmed) {
                    abp.ui.setBusy();
                    this._bopNodeServiceProxy.downloadBopBatchExport(
                        input
                    )
                        .subscribe(result => {
                            abp.ui.clearBusy();
                            if (result.code == 0) {
                                window.location.href = result.message;
                                this.getTable();
                            } else {
                                this._messageService.error(result.message);
                            }
                        });
                }
            }
        );
    }
    ngOnInit() {
    }
    //打开弹窗
    onShown(): void {
        this.getTreeInfo();
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.files1 = [];
        this.files2 = [];
        this.active = false;
        this.modal.hide();
    }
    nodeSelect(event) {
        this.getTable();
    }
    resert() {
        this.materialname = undefined;
        this.materialCode = undefined;
        this.propty = undefined;
    }
    getTable() {
        this._productionDesignServiceProxy.getTechListByNodeId(
            this.selectedFiles.id,
            this.materialname,
            this.materialCode,
            this.propty
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.files1 = result;
            });
    }
    //获取树结构
    getTreeInfo(): void {
        this.loadingtree = true;
        this._bopNodeServiceProxy.getTreeGrid(this.editData.id)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                let oldNode = this.files2;
                //建立父级节点
                this.files2 = [{
                    id: this.editData.id, key: -1, nodeDrawingNo: this.editData.drawingNo,
                    nodeName: this.editData.modelName, model: this.editData.model,
                    lotNo: this.editData.lotNo, stageSign: this.editData.stageSign,
                    children: result ? result : []
                }];
                this.selectedFiles = {};
                this.loadingtree = false;
                this.expandAll();
                this.selectedFiles.id = this.editData.id;
                this.getTable();
            });
    }
    expandAll() {
        this.files2.forEach((node) => {
            this.expandRecursive(node, true);
        });
    }
    private expandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.expandRecursive(childNode, isExpand);
            });
        }
    }
}